package com.mysteel.cyx.ddd.interfaces.web.response;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ApiResponse<T> {
    private int statusCode;
    private String message;
    private T data;

    private ApiResponse(ResponseCode responseCode) {
        this.statusCode = responseCode.getCode();
        this.message = responseCode.getMsg();
    }

    public static <T> ApiResponse<T> success() {
        return new ApiResponse<>(HttpStatus.OK.value(), "OK", null);
    }

    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(HttpStatus.OK.value(), "OK", data);
    }

    public static <T> ApiResponse<T> success(String msg, T data) {
        return new ApiResponse<>(HttpStatus.OK.value(), msg, data);
    }

    public static ApiResponse<Object> success(String msg) {
        return success(msg, null);
    }

    public static <T> ApiResponse<T> failed(ResponseCode responseCode) {
        return new ApiResponse<>(responseCode);
    }

    public static <T> ApiResponse<T> failed(String msg) {
        return new ApiResponse<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, null);
    }

    public static <T> ApiResponse<T> failed(Integer code, String msg) {
        return new ApiResponse<>(code, msg, null);
    }

    @Override
    public String toString() {
        return "ApiResponse{" +
                "statusCode=" + statusCode +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
